package org.kitrum.example.spring.config;

import org.kitrum.example.model.UserBean;
import org.kitrum.example.spring.factory.UserFactoryBean;
import org.kitrum.example.spring.factory.UserObjectFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.StringUtils;

/**
 * Spring 注解配置方式启动类
 *
 * @author : Kun
 * @version : 1.0
 * @date : 2021/01/21 18:54
 **/
public class AnnotationApplication {

    public static void run() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        BeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(UserFactoryBean.class);
        String beanName = beanNameGenerator.generateBeanName(builder.getBeanDefinition(),context);
        System.out.println(beanName);
        context.registerBeanDefinition(beanName, builder.getBeanDefinition());
        context.refresh();
        UserBean userBean = context.getBean(UserBean.class);
        UserBean userBean_2 = context.getBean(UserBean.class);
        userBean.print();
        System.out.println(userBean.equals(userBean_2));
    }

    public static void main(String[] args) {
        run();
    }
}
